/**
 * 1，时间戳转换为string类型（1668963884000 => 2022-11-21 1:4:44）
 */
export const timeChangeStr = (dateTime) => {
	//根据时间戳生成时间对象
	var d = new Date(dateTime)
	console.log(d)

	var date = (
			d.getFullYear()) + "-" + //获得年
		(d.getMonth() + 1) + "-" + //获得月，此处必须+1，因为Date类型中定义月份由0开始
		(d.getDate()) + " " + //获得日
		(d.getHours()) + ":" + //获得时
		(d.getMinutes()) + ":" + //获得分
		(d.getSeconds()); //获得秒
	return date;
}

/**
 * 2，string类型时间转换为时间戳（2022-11-21 1:4:44 => 1668963884000）
 */
export const timeChangeStamp = (dateTime) => {
	var d = new Date(dateTime)
	return d.getTime()
}



export const getNowTime = () => {
	let now = new Date()

	let t = now.getTime()
	let h = now.getHours()
	let m = now.getMinutes()
	let y = now.getFullYear()
	let M = now.getMonth()
	let d = now.getDate()
	let s = now.getSeconds()
	//console.log(s)

	var yf = 0;
	if (M > 10) {
		yf = M + 1
	} else {
		yf = '0' + (M + 1)
	}

	var result = y + "-" + yf + '-' + d + " " + h + ":" + m + ":" + s
	return result
}



//dateTimeStamp是一个时间毫秒，注意时间戳是秒的形式，在这个毫秒的基础上除以1000，就是十位数的时间戳。13位数的都是时间毫秒。
export const timeago = (dateTimeStamp) => {
	var minute = 1000 * 60; //把分，时，天，周，半个月，一个月用毫秒表示
	var hour = minute * 60;
	var day = hour * 24;
	var week = day * 7;
	var halfamonth = day * 15;
	var month = day * 30;
	var now = new Date().getTime(); //获取当前时间毫秒
	//console.log(now)
	var diffValue = now - dateTimeStamp; //时间差

	if (diffValue < 0) {
		return;
	}
	var result = ''
	var minC = diffValue / minute; //计算时间差的分，时，天，周，月
	var hourC = diffValue / hour;
	var dayC = diffValue / day;
	var weekC = diffValue / week;
	var monthC = diffValue / month;
	if (monthC >= 1 && monthC <= 3) {
		result = " " + parseInt(monthC) + "月前"
	} else if (weekC >= 1 && weekC <= 3) {
		result = " " + parseInt(weekC) + "周前"
	} else if (dayC >= 1 && dayC <= 6) {
		result = " " + parseInt(dayC) + "天前"
	} else if (hourC >= 1 && hourC <= 23) {
		result = " " + parseInt(hourC) + "小时前"
	} else if (minC >= 1 && minC <= 59) {
		result = " " + parseInt(minC) + "分钟前"
	} else if (diffValue >= 0 && diffValue <= minute) {
		result = "刚刚"
	} else {
		var datetime = new Date();
		datetime.setTime(dateTimeStamp);
		var Nyear = datetime.getFullYear();
		var Nmonth = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
		var Ndate = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
		var Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
		var Nminute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
		var Nsecond = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
		result = Nyear + "-" + Nmonth + "-" + Ndate
	}
	return result;
}

/**
 * 3，计算时间差距
 */
export const getDateToNewData = (datetime) => {
	//1，时间转换(生成时间对象)
	var d = new Date(dateTime)
	//2，获取年月日
	var dYear = d.getFullYear() //获得年
	var dMonth = d.getMonth() + 1 //获得月，此处必须+1，因为Date类型中定义月份由0开始
	var dDay = d.getDate() //获得日
	var dHours = d.getHours() //获得时
	var dMinutes = d.getMinutes() //获得分
	var dSeconds = d.getSeconds() //获得秒

	//3，获取当前时间
	var nowD = new Date()
	//4，获得当前时间对应的时间参数
	var nowDYear = nowD.getFullYear() //获得年
	var nowDMonth = nowD.getMonth() + 1 //获得月，此处必须+1，因为Date类型中定义月份由0开始
	var nowDDay = nowD.getDate() //获得日
	var nowDHours = nowD.getHours() //获得时
	var nowDMinutes = nowD.getMinutes() //获得分
	var nowDSeconds = nowD.getSeconds() //获得秒

	//5，比较
	//5.1，比较年份
	if (dYear !== nowDYear) {
		var result = dYear + '年' + dMonth + '月' + dDay + '日'
		return result
	}
	//5.2，比较月份
	if (dMonth !== nowDMonth) {
		var result = dMonth + '月' + dDay + '日'
		return result
	}
	//5.3，比较日
	if (dDay !== nowDDay) {
		if ((nowDDay - dDay) > 1) {
			var result = dMonth + '月' + dDay + '日'
			return result
		} else {
			return '昨天'
		}
	}
	//5.4，比较时
	if (dHours !== nowDHours) {
		// if ()
	}


}

//间隔时间差
export const spacTime = (old, now) => {
	console.log(old)
	var newOld = new Date(old);
	var newNow = new Date(now);
	var told = newOld.getTime()
	var tnow = newNow.getTime()

	console.log(told + 1000 * 60 * 1)
	console.log(tnow)
	//5分钟显示时间
	if ((told + 1000 * 60 * 1) < tnow) {
		console.log(told + 1000 * 60 * 1)
		return now
	} else {
		return ''
	}
}



//是否大于今天
export const spacToday = (time) => {
	var today = new Date()

	var upTimeYear = time.substring(0, 4)
	var upTimeMonth = time.substring(5, 7)
	var upTimeDay = time.substring(8, 10)
	if (upTimeYear > today.getFullYear()) {
		return false
	}
	if (upTimeYear == today.getFullYear()) {
		if (upTimeMonth > (today.getMonth() + 1)) {
			return false
		}
		if (upTimeMonth == (today.getMonth() + 1)) {
			if (upTimeDay > today.getDate()) {
				return false
			}
		}
	}
	return true

}


export const handleTime = (date) => {
	var time;
	var d = new Date(date);
	var n = new Date();
	//获取时间戳
	var dd = d.getTime();
	var h = d.getHours();
	var m = d.getMinutes();
	var Y = d.getFullYear();
	var M = d.getMonth() + 1;
	var D = d.getDate();
	//现在时间
	var nn = n.getTime();
	var hh = n.getHours();
	var mm = n.getMinutes();
	var YY = n.getFullYear();
	var MM = n.getMonth() + 1;
	var DD = n.getDate();

	if (D == DD && M == MM && Y == YY) {
		if (h < 10) {
			h = '0' + h;
		}
		if (m < 10) {
			m = '0' + m;
		}
		time = h + ':' + m;
		return time;
	} else if (D + 1 == DD && M == MM && Y == YY) {
		if (h < 10) {
			h = '0' + h;
		}
		if (m < 10) {
			m = '0' + m;
		}
		time = '昨天' + ' ' + h + ':' + m;
		return time;
	} else {
		if (M < 10) {
			M = '0' + M;
		}
		if (D < 10) {
			D = '0' + D;
		}
		if (h < 10) {
			h = '0' + h;
		}
		if (m < 10) {
			m = '0' + m;
		}
		time = Y + '年' + M + '月' + D + '日' + ' ' + h + ':' + m;
		return time;
	}
}

//时间格式化
export const handleNowTime = (time) => {
	let now = new Date(time)

	let t = now.getTime()
	let h = now.getHours()
	let m = now.getMinutes()
	let y = now.getFullYear()
	let M = now.getMonth()
	let d = now.getDate()
	let s = now.getSeconds()
	//console.log(s)

	var yf = 0;
	if (M >= 9) {
		yf = M + 1
	} else {
		yf = '0' + (M + 1)
	}

	var df = d;
	if (d < 10) {
		df = '0' + d
	}

	var hf = h;
	if (h < 10) {
		hf = '0' + h
	}

	var mf = m;
	if (m < 10) {
		mf = '0' + m
	}

	var sf = s;
	if (s < 10) {
		sf = '0' + s
	}


	var result = y + "-" + yf + '-' + df + " " + hf + ":" + mf + ":" + sf
	return result
}